# -*- coding: UTF-8 -*-  

import datetime
import sqlalchemy
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker,relationship
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, ForeignKey
from sqlalchemy.dialects.mysql import VARCHAR, BIGINT, LONGTEXT, INTEGER, DATETIME
from donato import app

# echo=False
engine = create_engine(app.config['DATABASE_URI'],pool_recycle=3600)

Session = sessionmaker(bind=engine)

Base = declarative_base()

class Droid(Base):
	__tablename__ = 'droid'

	id = Column(BIGINT(20), primary_key=True)
	name = Column(VARCHAR(255))
	alias = Column(VARCHAR(255))
	contact_flag = Column(INTEGER(11), nullable=False)
	display_name = Column(VARCHAR(255))
	head_img_url = Column(VARCHAR(255))
	member_count = Column(INTEGER(11), nullable=False)
	nick_name = Column(VARCHAR(255))
	province = Column(VARCHAR(255))
	city = Column(VARCHAR(255))
	remark_name = Column(VARCHAR(255))
	sex = Column(INTEGER(11), nullable=False)
	signature = Column(VARCHAR(255))
	username = Column(VARCHAR(255))

	groupmembers = relationship('GroupMember',backref = 'droid',lazy='dynamic')
	messages = relationship('Message',backref = 'droid', lazy='dynamic')
	contacters = relationship('Contacter',backref = 'droid', lazy='dynamic')

class GroupMember(Base):
	__tablename__ = 'group_member'

	id = Column(BIGINT(20), primary_key=True)
	alias = Column(VARCHAR(255))
	contact_flag = Column(INTEGER(11), nullable=False)
	head_img_url = Column(VARCHAR(255))
	display_name = Column(VARCHAR(255))
	member_count = Column(INTEGER(11), nullable=False)
	nick_name = Column(VARCHAR(255))
	province = Column(VARCHAR(255))
	city = Column(VARCHAR(255))
	remark_name = Column(VARCHAR(255))
	sex = Column(INTEGER(11), nullable=False)
	signature = Column(VARCHAR(255))
	last_update_time = Column(DATETIME)
	username = Column(VARCHAR(255))

	droid_id = Column(BIGINT(20), ForeignKey('droid.id'))
	group_id = Column(BIGINT(20), ForeignKey('contacter.id'))

	groupmentionlogs = relationship('GroupMentionLog',backref='groupmember',lazy='dynamic')
	message = relationship('Message',backref='groupmember',lazy='dynamic')

class GroupMentionLog(Base):
	__tablename__ = 'group_mention_log'

	id = Column(BIGINT(20), primary_key=True)
	group_member_name = Column(VARCHAR(255))
	group_name = Column(VARCHAR(255))
	message_content = Column(VARCHAR(255))
	message_time = Column(DATETIME)

	group_id = Column(BIGINT(20), ForeignKey('contacter.id'))
	group_member_id = Column(BIGINT(20), ForeignKey('group_member.id'))
	command_id = Column(BIGINT(20), ForeignKey('command.id'))

class Message(Base):
	__tablename__ = 'message'

	id = Column(BIGINT(20), primary_key=True)
	content = Column(LONGTEXT)
	message_type = Column(VARCHAR(255))
	message_time = Column(DATETIME)
	msg_id = Column(VARCHAR(255))
	group_member_name = Column(VARCHAR(255))
	receiver_name = Column(VARCHAR(255))
	sender_name = Column(VARCHAR(255))

	droid_id = Column(BIGINT(20), ForeignKey('droid.id'))

	group_member_id = Column(BIGINT(20), ForeignKey('group_member.id'))
	receiver_id = Column(BIGINT(20), ForeignKey('contacter.id'))
	sender_id = Column(BIGINT(20), ForeignKey('contacter.id'))
	

class Contacter(Base):
	__tablename__ = 'contacter'

	id = Column(BIGINT(20), primary_key=True)
	contacter_type = Column(VARCHAR(255))
	last_update_time = Column(DATETIME)
	alias = Column(VARCHAR(255))
	contact_flag = Column(INTEGER(11), nullable=False)
	display_name = Column(VARCHAR(255))
	head_img_url = Column(VARCHAR(255))
	member_count = Column(INTEGER(11), nullable=False)
	nick_name = Column(VARCHAR(255))
	province = Column(VARCHAR(255))
	city = Column(VARCHAR(255))
	remark_name = Column(VARCHAR(255))
	sex = Column(INTEGER(11), nullable=False)
	signature = Column(VARCHAR(255))
	hashcode = Column(VARCHAR(255))
	username = Column(VARCHAR(255))

	droid_id = Column(BIGINT(20), ForeignKey('droid.id'))

	groupmembers = relationship('GroupMember',backref='group',lazy='dynamic')
	groupmentionlogs = relationship('GroupMentionLog',backref='group',lazy='dynamic')
	received_messages = relationship('Message', foreign_keys='Message.receiver_id',backref='receiver', lazy='dynamic')
	sent_messages = relationship('Message', foreign_keys='Message.sender_id',backref='sender', lazy='dynamic')

class Command(Base):
	__tablename__ = 'command'

	id = Column(BIGINT(20), primary_key=True)
	keyword = Column(VARCHAR(255))
	template = Column(VARCHAR(255))
	template_type = Column(VARCHAR(255))
	alias = Column(VARCHAR(255))
	contacter_id = Column(BIGINT(20))

	groupmentionlogs = relationship('GroupMentionLog',backref='command',lazy='dynamic')


Base.metadata.create_all(engine)
